'''
Created on Aug 19, 2011

@author: rarakar
This program was used to verify the validity of the words in ASJP listss12. 
I checked it with listss14 and hand corrected all the words with the latest one.
Not sure how Eric's program works.
Still there are entries which has a space in a single word.
This program uses all the string similarity functions implemented
'''

import re, os, codecs, nltk
from asjp_util import cleanRow, processASJPWord, merge_1, fileToDM
import string_distances
import asjp_util
import numpy

alphabet={}
concepts={}
maxAlternativesStupid=2
#XXX=['X','X','X']
useLDND = True


def dm(ls):
	#print choice
	print list(string_distances.metricsMap.iterkeys())
	m={}
	levDist=0
	langs=language.split("%%%")
	langa=langs[0]
	langx=langa.replace("/","_")
	fout = codecs.open(langx+".txt","w","utf-8")
	rlangs=langs[1].split(",")
	for langb in rlangs:
		if langa in ls and langb in ls:
			if langs[0]!=langb:
			   va = ls[langs[0]]
			   vb = ls[langb]
			   levDist = ldnd(va,vb)
#			   print langa, langb, levDist
			fout.write(langs[0]+"\t"+ langb+"\t"+str(levDist)+"\n")
	return 1


def ldnd(la,lb):
	lv=l=nv=n=0
	count_same = 0.0
	count_diff = 0.0
	sum_same_arr = numpy.zeros(len(string_distances.metricsMap.keys()))
	sum_diff_arr = numpy.zeros(len(string_distances.metricsMap.keys()))
	for ka,va in la.iteritems():
		for kb,vb in lb.iteritems():
			if len(va) != 1 or va[0] != asjp_util.XXX:
				if len(vb) != 1 or vb[0] != asjp_util.XXX:
#					print ka, va, kb, vb, "\t"
					if ka == kb:
						count_same = count_same + 1
						sum_same_arr = numpy.add(sum_same_arr, ldna(va,vb))
							#l=l+1;lv=lv+ldna(va,vb)
					else:
						count_diff = count_diff + 1
						sum_diff_arr = numpy.add(sum_diff_arr, ldna(va,vb))
						
	sum_same_arr = sum_same_arr / float(count_same)
	sum_diff_arr = sum_diff_arr / float(count_diff) 
	sum_diff_arr = sum_same_arr / sum_diff_arr 
#	print count_same, count_diff, sum_same_arr, sum_diff_arr
	ldn_str = numpy.array2string(sum_same_arr, separator = ',', prefix="LDN")
	ldnd_str = numpy.array2string(sum_diff_arr, max_line_width=6, separator = ',', prefix=" LDND")  
						#n=n+1;nv=nv+ldna(va,vb)
	if count_diff == 0: return ldn_str
	return ldn_str + ldnd_str
	
#	if useLDND:
#		return (lv/float(l))/(nv/float(n))
#	else:
#		return lv/float(l)

def ldna(aa, bb):
	count_comb = 0.0
	r = 0
	sum_array = numpy.zeros(len(string_distances.metricsMap.keys()))
#	maxAStupid = maxAlternativesStupid
	for a in aa:
#		maxBStupid = maxAlternativesStupid
		for b in bb:
			tmp = numpy.array([string_distances.computeDistance(a, b, sm) for sm in string_distances.metricsMap.iterkeys()])
			sum_array = numpy.add(sum_array, tmp)
#			print "In ldna", a, b, tmp, sum_array
			count_comb = count_comb + 1.0
						
	if count_comb > 0:
#		print "In ldna: summing", sum_array, count_comb			
		sum_array = sum_array / float(count_comb)
	else:
		sum_array = numpy.zeros(len(string_distances.metricsMap.keys()))
		sum_array[0] = 1.0
#	print "sum array", aa, bb, sum_array
	return sum_array	
#			if maxAStupid > 0 and maxBStupid > 0:
#				if choice == 1:
#					r = r + string_distances.ldn(a, b)
#				elif choice == 2:
#					c = c + 1
#					r = r + string_distances.dice(a, b)
#				elif choice == 3:
#					c = c + 1
#					r = r + string_distances.lcs(a, b)
#				elif choice == 4:
#					c = c + 1
#					r = r + string_distances.jcd(a, b)
#				elif choice == 5:
#					c = c + 1
#					r = r + string_distances.prefix(a, b)
#				elif choice == 6:
#					c = c + 1
#					r = r + string_distances.xdice(a, b)
#				elif choice == 7:
#					c = c + 1
#					r = r + string_distances.ident(a, b)
#				elif choice == 8:
#					c = c + 1
#					r = r + string_distances.trigram(a, b)
#			maxBStupid = maxBStupid - 1
#		maxAStupid = maxAStupid - 1
#	if c == 0:
#		if choice ==1:
#			return 1
#		else:
#			return 0
#	if choice == 1:
#		return r / c
#	else:
#		return 1.0 - (r / c)

if __name__ == '__main__':
	import sys
#	fin = codecs.open("data/testin-soeren","r","cp1252")
	fin = codecs.open("/home/rarakar/GSLT/courses/ProgforNLP/Assn1/src/Project/data/listss14.txt","r","cp1252")
	global language
#	global choice
	language = sys.argv[1]
#	language = codecs.open("data/test",'r','utf-8').readline()
	lists = fileToDM(fin)
	dm(lists)
	#choice = 1
#	for i in range(1,9):
#		choice = i
		#print language
	
#	lists = fileToDM(fin)
#	fout = codecs.open("3730-coded.txt","w","utf-8")
#	print lists
#		fout.write(lang)
